package com.margo.project.aspectj.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;

@Service
public class MargoAcpectService {
	
	private Logger logger=LoggerFactory.getLogger(MargoAcpectService.class);

	/**
	 * fallback @return name of the fallback function, empty by default
	 * blockHandler @return name of the block exception function, empty by default
	 * @param orderId
	 * @return
	 */
	@SentinelResource(value = "getOrderInfo", blockHandler = "handleFlowQpsException",
            fallback = "queryOrderInfo2Fallback")
	public String queryOrderInfo(String orderId) {
		 // 模拟接口运行时抛出代码异常
        if ("000".equals(orderId)) {
            throw new RuntimeException();
        }
        logger.info("获取订单信息:" + orderId);
        return "return OrderInfo :" + orderId;
	}
	
	/**
     * 订单查询接口抛出限流或降级时的处理逻辑
     *
     * 注意: 方法参数、返回值要与原函数保持一致
     * @return
     */
    public String handleFlowQpsException(String orderId, BlockException e) {
        logger.info("订单查询接口抛出限流或降级时的处理逻辑", e);
        return "handleFlowQpsException for queryOrderInfo2: " + orderId;
    }

    /**
     * 订单查询接口运行时抛出的异常提供fallback处理
     *
     * 注意: 方法参数、返回值要与原函数保持一致
     * @return
     */
    public String queryOrderInfo2Fallback(String orderId, Throwable e) {
        return "fallback queryOrderInfo2: " + orderId;
    }


}
